home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / RCS / strtod.c,v < prev    next >
Encoding:
Text File  |  1989-03-22  |  6.4 KB  |  302 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     89.03.22.00.47.27;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     88.04.28.17.20.27;  author ouster;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @*** empty log message ***
  27. @
  28. text
  29. @/* 
  30.  * strtod.c --
  31.  *
  32.  *    Source code for the "strtod" library procedure.
  33.  *
  34.  * Copyright 1988 Regents of the University of California
  35.  * Permission to use, copy, modify, and distribute this
  36.  * software and its documentation for any purpose and without
  37.  * fee is hereby granted, provided that the above copyright
  38.  * notice appear in all copies.  The University of California
  39.  * makes no representations about the suitability of this
  40.  * software for any purpose.  It is provided "as is" without
  41.  * express or implied warranty.
  42.  */
  43.  
  44. #ifndef lint
  45. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/strtod.c,v 1.1 88/04/28 17:20:27 ouster Exp Locker: rab $ SPRITE (Berkeley)";
  46. #endif /* not lint */
  47.  
  48. #include <stdlib.h>
  49. #include <ctype.h>
  50.  
  51. #ifndef TRUE
  52. #define TRUE 1
  53. #define FALSE 0
  54. #endif
  55. #ifndef NULL
  56. #define NULL 0
  57. #endif
  58.  
  59. static int maxExponent = 511;    /* Largest possible base 10 exponent.  Any
  60.                  * exponent larger than this will already
  61.                  * produce underflow or overflow, so there's
  62.                  * no need to worry about additional digits.
  63.                  */
  64. static double powersOf10[] = {    /* Table giving binary powers of 10.  Entry */
  65.     10.,            /* is 10^2^i.  Used to convert decimal */
  66.     100.,            /* exponents into floating-point numbers. */
  67.     1.0e4,
  68.     1.0e8,
  69.     1.0e16,
  70.     1.0e32,
  71.     1.0e64,
  72.     1.0e128,
  73.     1.0e256
  74. };
  75.  
  76. /*
  77.  *----------------------------------------------------------------------
  78.  *
  79.  * strtod --
  80.  *
  81.  *    This procedure converts a floating-point number from an ASCII
  82.  *    decimal representation to internal double-precision format.
  83.  *
  84.  * Results:
  85.  *    The return value is the double-precision floating-point
  86.  *    representation of the characters in string.  If endPtr isn't
  87.  *    NULL, then *endPtr is filled in with the address of the
  88.  *    next character after the last one that was part of the
  89.  *    floating-point number.
  90.  *
  91.  * Side effects:
  92.  *    None.
  93.  *
  94.  *----------------------------------------------------------------------
  95.  */
  96.  
  97. double
  98. strtod(string, endPtr)
  99.     char *string;        /* A decimal ASCII floating-point number,
  100.                  * optionally preceded by white space.
  101.                  * Must have form "-I.FE-X", where I is the
  102.                  * integer part of the mantissa, F is the
  103.                  * fractional part of the mantissa, and X
  104.                  * is the exponent.  Either of the signs
  105.                  * may be "+", "-", or omitted.  Either I
  106.                  * or F may be omitted, or both.  The decimal
  107.                  * point isn't necessary unless F is present.
  108.                  * The "E" may actually be an "e".  E and X
  109.                  * may both be omitted (but not just one).
  110.                  */
  111.     char **endPtr;        /* If non-NULL, store terminating character's
  112.                  * address here. */
  113. {
  114.     int sign, expSign = FALSE;
  115.     double fraction, dblExp, *d;
  116.     register char *p, c;
  117.     int exp = 0;        /* Exponent read from "EX" field. */
  118.     int fracExp = 0;        /* Exponent that derives from the fractional
  119.                  * part.  Under normal circumstatnces, it is
  120.                  * the negative of the number of digits in F.
  121.                  * However, if I is very long, the last digits
  122.                  * of I get dropped (otherwise a long I with a
  123.                  * large negative exponent could cause an
  124.                  * unnecessary overflow on I alone).  In this
  125.                  * case, fracExp is incremented one for each
  126.                  * dropped digit.
  127.                  */
  128.     int mantSize;        /* Number of digits in mantissa. */
  129.     int decPt;            /* Number of mantissa digits BEFORE decimal
  130.                  * point.
  131.                  */
  132.     char *pExp;            /* Temporarily holds location of exponent
  133.                  * in string.
  134.                  */
  135.  
  136.     /*
  137.      * Strip off leading blanks and check for a sign.
  138.      */
  139.  
  140.     p = string;
  141.     while (isspace(*p)) {
  142.     p += 1;
  143.     }
  144.     if (*p == '-') {
  145.     sign = TRUE;
  146.     p += 1;
  147.     } else {
  148.     if (*p == '+') {
  149.         p += 1;
  150.     }
  151.     sign = FALSE;
  152.     }
  153.  
  154.     /*
  155.      * Count the number of digits in the mantissa (including the decimal
  156.      * point), and also locate the decimal point.
  157.      */
  158.  
  159.     decPt = -1;
  160.     for (mantSize = 0; ; mantSize += 1)
  161.     {
  162.     c = *p;
  163.     if (!isdigit(c)) {
  164.         if ((c != '.') || (decPt >= 0)) {
  165.         break;
  166.         }
  167.         decPt = mantSize;
  168.     }
  169.     p += 1;
  170.     }
  171.  
  172.     /*
  173.      * Now suck up the digits in the mantissa.  Use two integers to
  174.      * collect 9 digits each (this is faster than using floating-point).
  175.      * If the mantissa has more than 18 digits, ignore the extras, since
  176.      * they can't affect the value anyway.
  177.      */
  178.     
  179.     pExp  = p;
  180.     p -= mantSize;
  181.     if (decPt < 0) {
  182.     decPt = mantSize;
  183.     } else {
  184.     mantSize -= 1;            /* One of the digits was the point. */
  185.     }
  186.     if (mantSize > 18) {
  187.     fracExp = decPt - 18;
  188.     mantSize = 18;
  189.     } else {
  190.     fracExp = decPt - mantSize;
  191.     }
  192.     if (mantSize == 0) {
  193.     fraction = 0.0;
  194.     p = string;
  195.     goto done;
  196.     } else {
  197.     int frac1, frac2;
  198.     frac1 = 0;
  199.     for ( ; mantSize > 9; mantSize -= 1)
  200.     {
  201.         c = *p;
  202.         p += 1;
  203.         if (c == '.') {
  204.         c = *p;
  205.         p += 1;
  206.         }
  207.         frac1 = 10*frac1 + (c - '0');
  208.     }
  209.     frac2 = 0;
  210.     for (; mantSize > 0; mantSize -= 1)
  211.     {
  212.         c = *p;
  213.         p += 1;
  214.         if (c == '.') {
  215.         c = *p;
  216.         p += 1;
  217.         }
  218.         frac2 = 10*frac2 + (c - '0');
  219.     }
  220.     fraction = (1.0e9 * frac1) + frac2;
  221.     }
  222.  
  223.     /*
  224.      * Skim off the exponent.
  225.      */
  226.  
  227.     p = pExp;
  228.     if ((*p == 'E') || (*p == 'e')) {
  229.     p += 1;
  230.     if (*p == '-') {
  231.         expSign = TRUE;
  232.         p += 1;
  233.     } else {
  234.         if (*p == '+') {
  235.         p += 1;
  236.         }
  237.         expSign = FALSE;
  238.     }
  239.     while (isdigit(*p)) {
  240.         exp = exp * 10 + (*p - '0');
  241.         p += 1;
  242.     }
  243.     }
  244.     if (expSign) {
  245.     exp = fracExp - exp;
  246.     } else {
  247.     exp = fracExp + exp;
  248.     }
  249.  
  250.     /*
  251.      * Generate a floating-point number that represents the exponent.
  252.      * Do this by processing the exponent one bit at a time to combine
  253.      * many powers of 2 of 10. Then combine the exponent with the
  254.      * fraction.
  255.      */
  256.     
  257.     if (exp < 0) {
  258.     expSign = TRUE;
  259.     exp = -exp;
  260.     } else {
  261.     expSign = FALSE;
  262.     }
  263.     if (exp > maxExponent) {
  264.     exp = maxExponent;
  265.     }
  266.     dblExp = 1.0;
  267.     for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
  268.     if (exp & 01) {
  269.         dblExp *= *d;
  270.     }
  271.     }
  272.     if (expSign) {
  273.     fraction /= dblExp;
  274.     } else {
  275.     fraction *= dblExp;
  276.     }
  277.  
  278. done:
  279.     if (endPtr != NULL) {
  280.     *endPtr = p;
  281.     }
  282.  
  283.     if (sign) {
  284.     return -fraction;
  285.     }
  286.     return fraction;
  287. }
  288. @
  289.  
  290.  
  291. 1.1
  292. log
  293. @Initial revision
  294. @
  295. text
  296. @d17 2
  297. a18 2
  298. static char rcsid[] = "$Header: proto.c,v 1.2 88/03/11 08:39:08 ouster Exp $ SPRITE (Berkeley)";
  299. #endif not lint
  300. d20 1
  301. @
  302.